package com.hotel.user.service;

import com.hotel.common.entity.Permission;
import com.hotel.common.utils.Result;
import com.hotel.user.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    public Result<Permission> getPermissionById(Long id) {
        Permission permission = permissionMapper.findById(id);
        if (permission == null) {
            return Result.error("权限不存在");
        }
        return Result.success(permission);
    }

    public Result<Permission> getPermissionByCode(String permissionCode) {
        Permission permission = permissionMapper.findByPermissionCode(permissionCode);
        if (permission == null) {
            return Result.error("权限不存在");
        }
        return Result.success(permission);
    }

    public Result<List<Permission>> getAllPermissions() {
        List<Permission> permissions = permissionMapper.findAll();
        return Result.success(permissions);
    }

    public Result<List<Permission>> getPermissionsByRoleId(Long roleId) {
        List<Permission> permissions = permissionMapper.findByRoleId(roleId);
        return Result.success(permissions);
    }

    public Result<List<Permission>> getPermissionsByUserId(Long userId) {
        List<Permission> permissions = permissionMapper.findByUserId(userId);
        return Result.success(permissions);
    }

    public Result<Long> createPermission(Permission permission) {
        // 检查权限编码是否已存在
        Permission existingPermission = permissionMapper.findByPermissionCode(permission.getPermissionCode());
        if (existingPermission != null) {
            return Result.error("权限编码已存在");
        }
        
        Long permissionId = permissionMapper.insert(permission);
        return Result.success(permissionId);
    }

    public Result<Void> updatePermission(Permission permission) {
        Permission existingPermission = permissionMapper.findById(permission.getId());
        if (existingPermission == null) {
            return Result.error("权限不存在");
        }
        
        // 如果修改了权限编码，检查新编码是否已存在
        if (!existingPermission.getPermissionCode().equals(permission.getPermissionCode())) {
            Permission permissionWithSameCode = permissionMapper.findByPermissionCode(permission.getPermissionCode());
            if (permissionWithSameCode != null) {
                return Result.error("权限编码已存在");
            }
        }
        
        permissionMapper.update(permission);
        return Result.success();
    }

    public Result<Void> deletePermission(Long id) {
        Permission permission = permissionMapper.findById(id);
        if (permission == null) {
            return Result.error("权限不存在");
        }
        
        // 删除权限与角色的关联
        permissionMapper.deleteRolePermissionByPermissionId(id);
        // 删除权限
        permissionMapper.deleteById(id);
        return Result.success();
    }
}